package com.wuxinwudai.android.app;

import java.util.Stack;
import android.app.Activity;

/**
 * ActivityCollector 类为 Activity 回收器
 * @author 吾心无待 于 2016年04月10日
 *
 */
public class ActivityCollector{

	private static Stack<Activity> activities = new Stack<Activity>();

	/**
	 * 将 Activity 加入管理列表
	 * @param activity 要添加的 activity
	 */
	public static void addActivity(Activity activity){
		activities.add(activity);
	}

	/**
	 * 将 Activity 从管理列表移除
	 * @param activity 要移除的 activity
	 */
	public static void removeActivity(Activity activity){
		activities.remove(activity);
	}

	/**
	 * 结束所管理的 Activity
	 */
	public static void finishAll(){
		for(Activity activity : activities){
			if(!activity.isFinishing()){
				activity.finish();
			}
		}
		activities.clear();
	}

	/**
	 * 结束某个类型的 Activity
	 * @param cls 类型
	 */
	public static void finishActivities(Class<?> cls){
		for(Activity activity : activities){
			if (activity.getClass().equals(cls)){
				if(!activity.isFinishing()){
					activity.finish();
				}
				activities.remove(activity);
				break;
			}
		}
	}

	/**
	 * 结束某个 Activity
	 * @param activity 要结束的 Activity
	 */
	public static void finishActivity(Activity activity){
		if (activity != null){
			activities.remove(activity);
			activity.finish();
		}
	}

	/**
	 * 结束当前 Activity
	 */
	public static void finishActivity(){
		finishActivity(activities.lastElement());
	}

	/**
	 * 获取当前的 Activity
	 * @return
	 */
	public static Activity getCurrentActivity(){
		return activities.lastElement();
	}
}